package ua.eug.land3;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.*;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.model.NodePart;
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
import com.badlogic.gdx.graphics.g3d.utils.*;
import com.badlogic.gdx.utils.Array;

public class LandGame implements ApplicationListener {
	private PerspectiveCamera cam;
	public CameraInputController camController;
	
    private ModelBatch modelBatch;
    private Model model;
    private ModelInstance instance;
    public Array<ModelInstance> instances = new Array<ModelInstance>();
    private Environment environment;
    public AssetManager assets;
    public boolean loading;
    private Renderable terrain;
    private Renderable renderable;
    public RenderContext renderContext;
    public Shader shader;
    public Shader shader1;

    @Override

    public void create() {
    	modelBatch = new ModelBatch();
    	
    	cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    	cam.position.set(0f, -20f, -10f);
    	cam.lookAt(0,0,-5f);
    	cam.near = 1f;
    	cam.far = 300f;
    	cam.update();

        ModelBuilder modelBuilder = new ModelBuilder();
        model = modelBuilder.createBox(2f, 2f, 1f,
            new Material(ColorAttribute.createDiffuse(Color.BLUE)),
            Usage.Position | Usage.Normal);
        NodePart blockPart = model.nodes.get(0).parts.get(0);
        renderable = new Renderable();
        blockPart.setRenderable(renderable);
        renderable.environment = environment;
        renderable.worldTransform.idt();
        
    	terrain = new Renderable();
    	//HexTile tile = new HexTile();
    	Terrain tile = new Terrain(56,48);
    	terrain.mesh = tile.mesh;
    	terrain.meshPartOffset = 0;
    	terrain.meshPartSize = tile.mesh.getNumIndices(); // assuming you have indices
    	terrain.primitiveType = GL20.GL_TRIANGLES; // or GL_TRIANGLE_STRIP etc.
    	terrain.material = new Material();
    	
    	environment = new Environment();
    	environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.5f, 0.5f, 0.5f, 1f));
    	environment.add(new DirectionalLight().set(0.9f, 0.8f, 0f, -10f, 8f, -20f));
    	terrain.environment = environment;
    	
    	camController = new CameraInputController(cam);
        Gdx.input.setInputProcessor(camController);
        
        renderContext = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 1));
        shader = new DefaultShader(terrain);
        shader.init();
        shader1 = new DefaultShader(renderable);
        shader1.init();

    }
    
    @Override
    public void render () {
    	camController.update();

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
         
        renderContext.begin();
        shader.begin(cam, renderContext);
        shader.render(terrain);
        shader.end();
        shader1.begin(cam, renderContext);
        shader1.render(renderable);
        shader1.end();
        renderContext.end();
    }
     
    @Override
    public void dispose () {
    	modelBatch.dispose();
    	instances.clear();
        assets.dispose();
    }
     
    @Override
    public void resume () {
    }
 
    @Override
    public void resize (int width, int height) {
    }
 
    @Override
    public void pause () {
    }	 

}
